package com.irishud.hudnavi;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;

import com.irishud.hudnavi.wifi.WifiStatusService;

import java.io.FileReader;
import java.io.FileWriter;

public class MainActivity extends Activity {
    String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        final String TAG = "MainActivity";
        super.onCreate(savedInstanceState);
        final Button btnExit;
        setContentView(R.layout.activity_main);

        ResetMoto();

        if(BuildConfig.PRODUCT.contains("dw21")){
            launch_AR_apk();
            Intent intent = new Intent(MainActivity.this,NaviFloatWindowService.class);
            startService(intent);
            finish();
        }

        if(BuildConfig.PRODUCT.equals("x3")){
            Intent intent = new Intent(MainActivity.this,x3SimpleNaviActivity.class);
            startActivity(intent);

            Intent intent1= new Intent(MainActivity.this,CommService.class);
            startService(intent1);

            Intent intent2= new Intent(MainActivity.this,WifiStatusService.class);
            startService(intent2);
            finish();
        }

    }
    long oldTime = 0;
    public void ResetMoto(){
        new Thread(new Runnable(){
            @Override
            public void run() {
                oldTime = System.currentTimeMillis();

                while(true){

                    long nowTime = System.currentTimeMillis();
                    if((nowTime - oldTime) >= 1000 * 10){
                        break;
                    }
                    String  strMotoStatus = GetMotoStatus();
                    Log.i("BootBroadcastReceiver", "onReceive while(true):" +  strMotoStatus);

                    if(strMotoStatus.compareTo("HIGH") == 0){
                        try {
                            FileWriter fileWriter = new FileWriter("/sys/bus/platform/drivers/commom2phase/rwreg");
                            StringBuilder stringBuilder = new StringBuilder();
                            stringBuilder.append("1");
                            stringBuilder.append(" ");
                            stringBuilder.append("1");
                            fileWriter.write(stringBuilder.toString());
                            fileWriter.close();
                        }
                        catch (Exception e) {
                            e.printStackTrace();
                            Log.i("BootBroadcastReceiver", "Exception e");
                        }
                    }else{
                        break;
                    }

                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    public String GetMotoStatus(){

        String strFilePath = "/sys/bus/platform/drivers/commom2phase/rwreg";

        try{
            char []buf = new char[32];
            FileReader fileReader = new FileReader(strFilePath);
            int nLen = 0;
            if( (nLen = fileReader.read(buf)) != -1){
                String strValue = new String(buf, 0,4);
                Log.i(TAG, "GetMotoStatus:" + strValue + nLen);
                if(strValue.compareTo ("HIGH") == 0){
                    Log.i(TAG, "compareTo:" + strValue);
                    return "HIGH";
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return "LOW";
    }


    void launch_AR_apk(){
        try {
            android.util.Log.e(TAG,"boot Ar apk.....");
            Intent intent = new Intent(Intent.ACTION_VIEW);
            ComponentName componentName =
                    new ComponentName("org.sjtu_dalab.windshield_ar",
                            "com.unity3d.player.UnityPlayerActivity");//这个没问题
            intent.setComponent(componentName);
            startActivity(intent);
        }catch (Exception e){
        }
    }
}
